import request from '@/utils/request.ts'
import type { PagesData, ResponseData } from '@/api/type.ts'
import type {
  SPU,
  BaseSpuSaleAttr,
  spuImage,
  spuSaleAttr,
} from '@/api/product/spu/type.ts'

enum API {
  SPU_LIST_URL = '/admin/product',
  SPU_INFO_URL = '/admin/product/getSpuInfo',
  SPU_SAVE_URL = '/admin/product/saveSpuInfo',
  SPU_UPDATE_URL = '/admin/product/updateSpuInfo',
  SPU_DELETE_URL = '/admin/product/deleteSpu',
  SPU_ALL_SALE_ATTR_LIST_URL = '/admin/product/baseSaleAttrList',
  SPU_SALE_ATTR_LIST_URL = '/admin/product/spuSaleAttrList',
  SPU_IMAGE_LIST_URL = '/admin/product/spuImageList',
}

export const reqSpuList = (page: number, limit: number, c3Id: number) => {
  return request.get<any, ResponseData<PagesData<SPU>>>(
    `${API.SPU_LIST_URL}/${page}/${limit}?category3Id=${c3Id}`,
  )
}

export const reqSpuInfo = (id: number) => {
  return request.get<any, ResponseData<SPU>>(`${API.SPU_INFO_URL}/${id}`)
}

export const reqAddOrUpdateSpu = (spu: SPU) => {
  // 如果SPU对象拥有ID,更新已有的SPU
  if (spu.id) {
    return request.post<any, ResponseData<any>>(API.SPU_UPDATE_URL, spu)
  } else {
    return request.post<any, ResponseData<any>>(API.SPU_SAVE_URL, spu)
  }
}

export const reqSpuAllSaleAttrList = () => {
  return request.get<any, ResponseData<BaseSpuSaleAttr[]>>(
    API.SPU_ALL_SALE_ATTR_LIST_URL,
  )
}

export const reqDeleteSpu = (id: number) => {
  return request.delete<any, ResponseData<any>>(`${API.SPU_DELETE_URL}/${id}`)
}

export const reqSpuSaleAttrList = (spuId: number) => {
  return request.get<any, ResponseData<spuSaleAttr[]>>(
    `${API.SPU_SALE_ATTR_LIST_URL}/${spuId}`,
  )
}

export const reqSpuImageList = (spuId: number) => {
  return request.get<any, ResponseData<spuImage[]>>(
    `${API.SPU_IMAGE_LIST_URL}/${spuId}`,
  )
}
